/**
 * CopyRight  2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpException;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 喻金龙
 * <br />邮箱: yu.jinlong@ustcinfo.com
 * <br />描述: test.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-30 上午9:23:50
 * <br />CopyRight  2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

public class test {
	
	private static String urls[]={
		"http://211.138.191.86:80/nbspweb/webservice/leader!doWebservice.action?cmd=wlbdemographic&page=1&pagesize=100&usercode=fanbingheng&date=2013-8" ,
		"http://211.138.191.86/nbspweb/webservice/leader!doWebservice.action?cmd=wlbcompanyrc&page=1&pagesize=50&usercode=fanbingheng&date=2013-8",
		"http://211.138.191.86:80/nbspweb/webservice/leader!doWebservice.action?cmd=wlbstationresources&dimension=city&page=1&pagesize=100&usercode=fanbingheng&date=2013-6&regionid=",
		"http://211.138.191.86:80/nbspweb/webservice/leader!doWebservice.action?cmd=wlbstationresources&dimension=company&page=1&pagesize=100&usercode=fanbingheng&date=2013-6&regionid=",
		"http://211.138.191.86:80/nbspweb/webservice/leader!doWebservice.action?cmd=wlblineresources&dimension=city&page=1&pagesize=100&usercode=fanbingheng&date=2013-6&regionid=",
		"http://211.138.191.86:80/nbspweb/webservice/leader!doWebservice.action?cmd=wlblineresources&dimension=company&page=1&pagesize=100&usercode=fanbingheng&date=2013-6&regionid=",
		"http://211.138.191.86/nbspweb/webservice/leader!doWebservice.action?cmd=wlbpatrol&usercode=fanbingheng&date=2013-2&major=C31&page=1&pagesize=100&dimension=city&regionid=&flag=",
		"http://211.138.191.86:80/nbspweb/webservice/leader!doWebservice.action?cmd=wlbpatrol&usercode=fanbingheng&date=2013-2&major=C31&page=1&pagesize=100&dimension=company&regionid=&flag=",
		"http://211.138.191.86:80/nbspweb/webservice/leader!doWebservice.action?cmd=dangerreport&type=0&page=1&pagesize=30&usercode=fanbingheng&date=2013-9&regionid=",
		"http://211.138.191.86:80/nbspweb/webservice/leader!doWebservice.action?cmd=wlbcityquality&major=C31&page=1&pagesize=100&usercode=fanbingheng&date=2013-8&regionid=",
		"http://211.138.191.86:80/nbspweb/webservice/leader!doWebservice.action?cmd=wlbcompanyquality&major=C31&page=1&pagesize=100&usercode=fanbingheng&date=2013-8&regionid=",
		"http://211.138.191.86:80/nbspweb/webservice/leader!doWebservice.action?cmd=wlbassessment&usercode=fanbingheng&date=2013-8&major=C30&page=1&pagesize=100&dimension=city&regionid=",
		"http://211.138.191.86:80/nbspweb/webservice/leader!doWebservice.action?cmd=wlbassessment&usercode=fanbingheng&date=2013-8&major=C37&page=1&pagesize=100&dimension=city&regionid=",
		"http://211.138.191.86:80/nbspweb/webservice/leader!doWebservice.action?cmd=wlbcitycost&dimension=city&starttime=2013-4-1&endtime=2013-6-30&page=1&pagesize=300&usercode=fanbingheng&regionid=",
		"http://211.138.191.86:80/nbspweb/webservice/leader!doWebservice.action?cmd=wlbcitycost&dimension=major&starttime=2013-4-1&endtime=2013-6-30&page=1&pagesize=300&usercode=fanbingheng&regionid="
	};	
	
	@Autowired
	public  static String getRs() throws IOException, HttpException {
		String rs="";
		for(int i=0;i<urls.length;i++){
//		    String fileName="C:\\correct"+i+".txt";  //文件名
			String fileName="/opt/application/dwFile/correct"+i+".txt";  //文件名
			File file = new File(fileName);      //创建文件对象
			checkFile(file,i); //检查文件是否存在  如果存在不操作 如果不存在创建并写入
			String readS=readCorrectFile(file); //读出文件内容
			String result = getResult(urls[i]);
			if(readS.equals(result)){
				rs +="1,";
			}else{
				rs +="0,";
			}
		}
		System.out.println("=================返回"+urls.length+"===================");
		System.out.println(rs);
		System.out.println("=================返回===================");
		return rs;
	}

	private static void checkFile(File file,int i) throws IOException {
		if(!file.exists()){
		   file.createNewFile(); //如果不存在 则创建
		   writeCorrectFile(file,i);
		}	
	}
	
	private static void writeCorrectFile(File file,int i) {
		String result=null;
		try {
		 result = getResult(urls[i]);
		} catch (HttpException e) {
			e.printStackTrace();
		}
		
		try {
			FileOutputStream fos = new FileOutputStream(file);//这里可以有额外参数设置文件的写入方式
			fos.write(result.getBytes());
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}	  
		
	}
	
	private static String readCorrectFile(File file) throws IOException{
	      String s ="";
	      BufferedReader br;
		  br = new BufferedReader(new FileReader(file));
		  s = br.readLine();	  
		  br.close();	
	      return s;
	}
	
	public static String getResult(String url) throws HttpException {
		HttpClient httpClient = new HttpClient();
//		httpClient.getHostConfiguration().setProxy("proxy.ah.cmcc", 8080);
//		httpClient.getParams().setAuthenticationPreemptive(true);
		//创建POST方法的实例
		PostMethod postMethod = new PostMethod(url);
		//使用系统提供的默认的恢复策略
		postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
		String result = null; //初始化返回结果(String类型)
		byte[] responseBody = null; //初始化返回结果(byte[]类型)
		try {
			//执行getMethod
			int statusCode = httpClient.executeMethod(postMethod);
		    if (statusCode != HttpStatus.SC_OK) {
			    System.err.println("Method failed: " + postMethod.getStatusLine());
		    }
		    //返回结果(byte[]类型) 
		    responseBody = postMethod.getResponseBody();
		    //返回结果(String类型,GBK格式)
		    result = new String(responseBody, "UTF-8");
		} catch (IOException e) {
		   e.printStackTrace();
		} finally {
			//释放连接
			postMethod.releaseConnection();
		}
		return result;
	}
}